// 回收站
var deleteOffset = $(".delete").offset();
var delParam = {
  left: deleteOffset.left,
  top: deleteOffset.top,
  right: deleteOffset.left + $(".delete").width(),
  bottom: deleteOffset.top + $(".delete").height()
};

// 仓库
var warehouseOffset = $(".ku-control").offset();
var warehouseParam = {
  left: warehouseOffset.left,
  top: warehouseOffset.top,
  right: warehouseOffset.left + $(".ku-control").width(),
  bottom: warehouseOffset.top + $(".ku-control").height()
};

var headerTop = $('.trade-con .con-header').offset().top

function touchEnd(ele, tableParam) {
  // 删除该宠物
  var eleOffset = ele.offset();
  eleOffset.right = eleOffset.left + ele.width();
  eleOffset.bottom = eleOffset.top + ele.height();
  if (
    delParam.left - eleOffset.left <= ele.width() / 2 &&
    eleOffset.left - delParam.right <= ele.width() / 2 &&
    delParam.top - eleOffset.top <= ele.height() / 2 &&
    eleOffset.bottom - delParam.bottom <= ele.height() / 2
  ) {
    // 展示弹窗
    $(".delete-sure-wrapper")
      .attr("data-id", ele.attr("id"))
      .find(".animal")
      .attr("src", ele.find(".animal").attr("src"))
      .parents(".delete-sure-wrapper")
      .show();
    $(".mask").show();
    dialogId = "delete-sure-wrapper";
    return;
  }

  // 添加到仓库
  else if (
    warehouseParam.left - eleOffset.left <= ele.width() / 2 &&
    eleOffset.right - warehouseParam.right <= ele.width() / 2 &&
    warehouseParam.top - eleOffset.top <= ele.height() / 2 &&
    eleOffset.bottom - warehouseParam.bottom <= ele.height() / 2
  ) {
    // 展示弹窗
    $(".ku-sure-wrapper")
      .attr("data-id", ele.attr("id"))
      .find(".animal")
      .attr("src", ele.find(".animal").attr("src"))
      .parents(".ku-sure-wrapper")
      .show();
    $(".mask").show();
    dialogId = "ku-sure-wrapper";
  }

  // 滑到顶部背景，替换宠物
  else if (headerTop - eleOffset.bottom >= -ele.height() / 2) {
    var animalSrc = ele.find(".animal").attr('src').split("/m")[1].split(".png")[0];
    var arr = animalSrc.split('-');

    // 顶级
    if (arr[0] > 38) {
      $('.top-bg').show();
      $('.normal-bg').hide();

      // 不是顶级
    } else {
      $('.normal-bg').show();
      $('.top-bg').hide();
    }

    $('.move-pet').attr('src', './imgs/pet/' + arr[0] + '.gif').show();
    var id = ele.attr("id");

    ele.css({
      transition: "ease 300ms",
      transform: ele.attr("data-transform")
    });
  }

  // 超出格子，返回原位
  else if (
    tableParam.top - eleOffset.top > ele.width() / 2 ||
    eleOffset.bottom - tableParam.bottom > ele.width() / 2
  ) {
    ele.css({
      transition: "ease 300ms",
      transform: ele.attr("data-transform")
    });
  } else {
    touchEndCallback(ele);
  }
}